package cn.initcap.concurrency.concurrency.synccontainer;

import cn.initcap.concurrency.annoations.NotThreadSafe;
import java.util.Vector;

/**
 * 同步容器并不是一定线程安全的，例如这个例子。
 *
 * @author initcap
 * @date Created in 2018/6/17 AM10:25.
 */
@NotThreadSafe
public class VectorExample2 {

    private static final int THREAD_SIZE = 10;
    private static Vector<Integer> vector = new Vector<>();

    public static void main(String[] args) {
        while (true) {
            for (int i = 0; i < THREAD_SIZE; i++) {
                vector.add(i);
            }

            Thread thread1 = new Thread() {
                @Override
                public void run() {
                    for (int i = 0; i < vector.size(); i++) {
                        vector.remove(i);
                    }
                }
            };

            Thread thread2 = new Thread() {
                @Override
                public void run() {
                    for (int i = 0; i < vector.size(); i++) {
                        vector.get(i);
                    }
                }
            };
            thread1.start();
            thread2.start();
        }
    }

}
